package com.ttai.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerAddIdAcountComponet;
import com.ttai.dagger.module.activity.AddIdAcountPresenterModule;
import com.ttai.presenter.activity.AddIdAcountPresenter;
import com.ttai.presenter.activity.QrLoginPresenter;
import com.ttai.presenter.activity.TaiRegiserPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.ui.other.MessageDialog;
import com.ttai.untils.Constant;
import com.ttai.untils.ShareCodeUtil;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yzq.zxinglibrary.android.CaptureActivity;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddIdAcount extends BaseActivity {
    @Bind(R.id.sp_list)
    Spinner spList;
    @Bind(R.id.ed_idacountnumber)
    EditText edIdacountnumber;
    @Bind(R.id.ed_idacountpassword)
    EditText edIdacountpassword;
    @Bind(R.id.btn_logincheck)
    Button btnLogincheck;

    @Inject
    AddIdAcountPresenter addIdAcountPresenter;

    TaiRegiserPresenter taiRegiserPresenter;


    @Bind(R.id.btn_regiser)
    Button btnRegiser;
//    @Bind(R.id.btn_saomiao)
//    Button btnSaomiao;
    private String appId;
    public MessageDialog messageDialog;

    public ArrayList<String> idList = new ArrayList<>();
    public ArrayAdapter<String> adapter = null;
    public String nameNumber;
    public String mynumber;
    public String myPassword;
    public String jiamiPassword;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idacount);

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView barTitle = (TextView)findViewById(R.id.barTitle);
        barTitle.setText("托管登录");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ButterKnife.bind(this);
        setComponet();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        idList = addIdAcountPresenter.getListData();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, idList);
        //设置下拉列表风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spList.setAdapter(adapter);
        spList.setVisibility(View.VISIBLE);//设置默认显示


    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void onStart() {

        super.onStart();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setComponet() {
        DaggerAddIdAcountComponet.builder()
                .addIdAcountPresenterModule(new AddIdAcountPresenterModule(this))
                .build()
                .in(this);
    }

//    @OnClick(R.id.btn_logincheck)
//    public void onViewClicked() {
//        nameNumber = spList.getSelectedItem().toString();
//        String appId = addIdAcountPresenter.getAPPId(nameNumber);
//        mynumber = edIdacountnumber.getText().toString();
//        myPassword = edIdacountpassword.getText().toString();
//        try {
//            jiamiPassword = Constant.mD5Code(myPassword);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }
//
//
//        String token = Constant.getToken();
//        addIdAcountPresenter.check(appId, token, mynumber, jiamiPassword);
//    }

    public void showMessageDialog(String message) {
        messageDialog = new MessageDialog(AddIdAcount.this);
        messageDialog.setTitle("提示");
        messageDialog.setMessage(message);
        messageDialog.setYesOnclickListener("确定", new MessageDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
//                Toast.makeText(AddIdAcount.this,"点击了--确定--按钮",Toast.LENGTH_LONG).show();
                messageDialog.dismiss();
            }
        });
        messageDialog.setNoOnclickListener("取消", new MessageDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
//                Toast.makeText(AddIdAcount.this,"点击了--取消--按钮",Toast.LENGTH_LONG).show();
                messageDialog.dismiss();
            }
        });
        messageDialog.show();
    }

    @OnClick({R.id.btn_logincheck, R.id.btn_regiser})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_logincheck:
                nameNumber = spList.getSelectedItem().toString();
                appId = addIdAcountPresenter.getAPPId(nameNumber);
                mynumber = edIdacountnumber.getText().toString();
                myPassword = edIdacountpassword.getText().toString();
                try {
                    jiamiPassword = Constant.mD5Code(myPassword);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }

                String token = Constant.getToken();
                System.out.println("appId:" + appId + " token:" + token + " mynumber:" + mynumber + " jia:" + jiamiPassword);
                addIdAcountPresenter.check(appId, token, mynumber, jiamiPassword);
                break;
            case R.id.btn_regiser:
                nameNumber = spList.getSelectedItem().toString();
                appId = addIdAcountPresenter.getAPPId(nameNumber);
                Log.i("456", "onViewClicked: appId"+appId);
                if (appId != null){
                    System.out.println("appId:" + appId);
                    taiRegiserPresenter = new TaiRegiserPresenter(this);
                    taiRegiserPresenter.regiser(Constant.getToken(), appId);
                }else {
                    Toast.makeText(AddIdAcount.this,"账号已存在",Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

//    @OnClick(R.id.btn_saomiao)
//    public void onViewClicked() {
//
//        AndPermission.with(this)
//                .permission(com.yanzhenjie.permission.Permission.CAMERA, com.yanzhenjie.permission.Permission.READ_EXTERNAL_STORAGE)
//                .onGranted(new Action() {
//                    @Override
//                    public void onAction(List<String> permissions) {
//                        Intent intent = new Intent(AddIdAcount.this, CaptureActivity.class);
//                        /*ZxingConfig是配置类
//                         *可以设置是否显示底部布局，闪光灯，相册，
//                         * 是否播放提示音  震动
//                         * 设置扫描框颜色等
//                         * 也可以不传这个参数
//                         * */
////                                ZxingConfig config = new ZxingConfig();
////                                config.setPlayBeep(false);//是否播放扫描声音 默认为true
////                                config.setShake(false);//是否震动  默认为true
////                                config.setDecodeBarCode(false);//是否扫描条形码 默认为true
////                                config.setReactColor(R.color.colorAccent);//设置扫描框四个角的颜色 默认为白色
////                                config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
////                                config.setScanLineColor(R.color.colorAccent);//设置扫描线的颜色 默认白色
////                                config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
////                                intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
//                        startActivityForResult(intent, REQUEST_CODE_SCAN);
//                    }
//                })
//                .onDenied(new Action() {
//                    @Override
//                    public void onAction(List<String> permissions) {
//                        Uri packageURI = Uri.parse("package:" + getPackageName());
//                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//
//                        startActivity(intent);
//
//                        Toast.makeText(AddIdAcount.this, "没有权限无法扫描呦", Toast.LENGTH_LONG).show();
//                    }
//                }).start();
//    }
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//
//        // 扫描二维码/条码回传
//        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
//            if (data != null) {
//
//                String content = data.getStringExtra(com.yzq.zxinglibrary.common.Constant.CODED_CONTENT);
//                try {
//                    String context = Constant.getFromBASE64(content);
//                    System.out.println("context"+context);
//                    List<String> result = Arrays.asList(context.split(","));
//                    String qrId = result.get(0);
//                    String appId = result.get(1);
//                    System.out.println("qrId"+qrId+" appId"+appId);
//                    System.out.println("flag:"+ShareCodeUtil.codeToId(appId));
////                    qrLoginPresenter.qrLogin();
//                    Intent intent = new Intent(this,ChooseId.class);
//                    intent.putExtra("qrId",qrId);
//                    intent.putExtra("appId",appId);
//                    startActivity(intent);
//                }catch (Exception e){
//                    Toast.makeText(this,"无效二维码",Toast.LENGTH_SHORT).show();
//                }
//
//            }
//        }
//    }




//    @OnClick({R.id.btn_logincheck, R.id.btn_regiser})
//    public void onViewClicked(View view) {
//        nameNumber = spList.getSelectedItem().toString();
//        appId = addIdAcountPresenter.getAPPId(nameNumber);
//        switch (view.getId()) {
//            case R.id.btn_logincheck:
//                mynumber = edIdacountnumber.getText().toString();
//                myPassword = edIdacountpassword.getText().toString();
//                try {
//                    jiamiPassword = Constant.mD5Code(myPassword);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                } catch (NoSuchAlgorithmException e) {
//                    e.printStackTrace();
//                }
//
//                String token = Constant.getToken();
//                addIdAcountPresenter.check(appId, token, mynumber, jiamiPassword);
//                break;
//
//            case R.id.btn_regiser:
//                System.out.println("appId:"+appId);
//                taiRegiserPresenter = new TaiRegiserPresenter(this);
//                taiRegiserPresenter.regiser(Constant.getToken(), appId);
//                break;
//        }
//    }
}
